<?php

namespace App\Web\Model;

use App\Web\Model\User as UserModel;
use EchoPHP\Facades\Db;
use EchoPHP\Validator\Validator;


/**
 * Class TaskDetail
 * 任务详情管理相关业务逻辑
 * @package App\Web\Model
 */
class TaskDetail extends Model {
    /**
     * 查询总记录数
     * @param array $search
     * @return array
     */
    public function getListCount($loginId,$search = [],$isTeam=false) {
        $field = 'count(1) as count';
        return $this->selinfo($loginId,$search,$field,0,0,'',$isTeam)[0]['count'];
    }
    /**
     * 我收到的任务
     * @param $param
     * @return array
     */
    public function selinfo($loginId,$search = [],$field='', $rowFrom = 0, $rowTo = 0, $orderBy = '',$isTeam = false) {
        $field = empty($field)?"taskdetailid,receiver,a.taskid,b.startdate,b.enddate,b.taskname,b.isfullday,b.memo,b.tasktype,receivername,b.projectname,b.submittername,a.status,b.attachpath,b.attachname":$field;
        $sql = 'select '.$field.' from TaskDetail a LEFT JOIN Task b on a.taskid = b.taskid ';
        $var = [];
        $where = [];
        if(!$isTeam){
            $var[] = $loginId;
            $where[] = 'a.receiver = ?';
        }else{
            $oprModel = new UserModel();
            $oprInfo = $oprModel->getInfo($loginId, 'userid,teamcode,teamid');
            if (empty($oprInfo)) {
                throw new \RuntimeException('登录账号出现异常');
            }

            $sql .= ' left join User c on a.receiver = c.userid ';
            $where[] = ' c.teamcode like ? ';
            $var[] = $oprInfo['teamcode'] . '%';

        }

        //查询任务名称/ID
        if(isset($search['taskname']) && !empty($search['taskname'])){
            $nameParam = $this->searchStringOrNum($search['taskname'], 'a.taskname', 'a.taskid');
            $where[] = $nameParam['where'];
            $var = array_merge($var, $nameParam['var']);
        }
        //任务类型
        if(isset($search['tasktype']) && !empty($search['tasktype'])){
            $where[] = 'a.tasktype = ?';
            $var[] = $search['tasktype'];
        }

        //任务时间
        if(isset($search['startdate']) && !empty($search['startdate'])){
            $where[] = 'a.startdate >= ? ';
            $var[] = $search['startdate'] . ' 00:00:00';
        }
        if(isset($search['enddate']) && !empty($search['enddate'])){
            $where[] = ' a.enddate <= ? ';
            $var[] = $search['enddate'] . ' 23:59:59';
        }

        //任务时间
        if(isset($search['searchreceiver']) && !empty($search['searchreceiver'])){
            $where[] = 'a.receivername = ? ';
            $var[] = $search['searchreceiver'];
        }

        //状态已完成，未完成
        if(isset($search['status']) && !empty($search['status'])){
            $where[] = 'a.status = ?';
            $var[] = $search['status'];
        }else{
            $where[] = 'a.status != ?';
            $var[] = "06";
        }
        $sql .= ' WHERE ' . implode(' AND ', $where);
        $sql .= empty($orderBy) ? ' ORDER BY a.taskdetailid DESC' : (' ORDER BY ' . $orderBy);
        return Db::select($sql, $var, $rowFrom, $rowTo);
    }
}